home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ShareWare OnLine 2
/
ShareWare OnLine Volume 2 (CMS Software)(1993).iso
/
os2
/
remin301.zip
/
REMIN300.ZIP
/
UTILS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-10
|
7KB
|
220 lines
/***************************************************************/
/* */
/* UTILS.C */
/* */
/* Useful utility functions. */
/* */
/* This file is part of REMIND. */
/* Copyright (C) 1991 by David F. Skoll. */
/* */
/***************************************************************/
#include <string.h>
#include <stdio.h>
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#define UPPER(c) ( ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c) )
/***************************************************************/
/* */
/* StrEq */
/* */
/* Are two strings equal? */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC int StrEq(const char *s1, const char *s2)
#else
int StrEq(s1, s2)
char *s1, *s2;
#endif
{
while (*s1 && *s2) {
if (*s1++ != *s2++) return 0;
}
if (*s1 || *s2) return 0 ; else return 1;
}
/***************************************************************/
/* */
/* StriEq */
/* */
/* Are two strings equal, ignoring case? */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC int StriEq(const char *s1, const char *s2)
#else
int StriEq(s1, s2)
char *s1, *s2;
#endif
{
while (*s1 && *s2) {
if (UPPER(*s1) != UPPER(*s2)) return 0;
s1++;
s2++;
}
if (*s1 || *s2) return 0 ; else return 1;
}
/***************************************************************/
/* */
/* StrinEq */
/* Are two strings equal to a given number of chars? */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC int StrinEq(const char *s1, const char *s2, int n)
#else
int StrinEq(s1, s2, n)
char *s1, *s2;
int n;
#endif
{
while (*s1 && *s2 && n--) {
if (UPPER(*s1) != UPPER(*s2)) return 0;
s1++;
s2++;
}
if (n && (*s1 || *s2)) return 0 ; else return 1;
}
/***************************************************************/
/* */
/* StrnCpy */
/* */
/* Just like strncpy EXCEPT we ALWAYS copy the trailing 0. */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC char *StrnCpy(char *dest, const char *source, int n)
#else
char *StrnCpy(dest, source, n)
char *dest, *source;
int n;
#endif
{
register char *odest = dest;
while (n-- && (*dest++ = *source++)) ;
if (*(dest-1)) *dest = 0;
return odest;
}
/***************************************************************/
/* */
/* StrMatch */
/* */
/* Checks that two strings match (case-insensitive) to at */
/* least the specified number of characters, or the length */
/* of the first string, whichever is greater. */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC int StrMatch(const char *s1, const char *s2, int n)
#else
int StrMatch(s1, s2, n)
char *s1, *s2;
int n;
#endif
{
int l;
if ((l = strlen(s1)) < n) return 0;
return StrinEq(s1, s2, l);
}
/***************************************************************/
/* */
/* StrinCmp - compare strings, case-insensitive */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC int StrinCmp(const char *s1, const char *s2, int n)
#else
int StrinCmp(s1, s2, n)
char *s1, *s2;
int n;
#endif
{
register int r;
while (n && *s1 && *s2) {
n--;
r = UPPER(*s1) - UPPER(*s2);
if (r) return r;
s1++;
s2++;
}
if (n) return (UPPER(*s1) - UPPER(*s2)); else return 0;
}
/***************************************************************/
/* */
/* StrDup */
/* */
/* Like ANSI strdup */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC char *StrDup(const char *s)
#else
char *StrDup(s)
char *s;
#endif
{
char *ret = (char *) malloc(strlen(s)+1);
if (!ret) return (char *) NULL;
strcpy(ret, s);
return ret;
}
/***************************************************************/
/* */
/* StrCmpi */
/* */
/* Compare strings, case insensitive. */
/* */
/***************************************************************/
#ifdef HAVE_PROTOS
PUBLIC int StrCmpi(char *s1, char *s2)
#else
int StrCmpi(s1, s2)
char *s1, *s2;
#endif
{
int r;
while (*s1 && *s2) {
r = UPPER(*s1) - UPPER(*s2);
if (r) return r;
s1++;
s2++;
}
return UPPER(*s1) - UPPER(*s2);
}
#ifdef NO_STRSTR
#ifdef HAVE_PROTOS
PUBLIC char *strstr(char *s1, char *s2)
#else
char *strstr(s1, s2)
char *s1, *s2;
#endif
{
char *s = s1;
int len2 = strlen(s2);
int len1 = strlen(s1);
while (s-s1 <= len1-len2) {
if (!strncmp(s, s2, len2)) return s;
s++;
}
return NULL;
}
#endif